package a66weding.com.jiehuntong.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.AddressEvent;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;

/**
 * 这是我的界面下修改收货地址的界面
 * Created by Administrator on 2016/7/21.
 */
public class MeCompileAddressActivity extends MeBaseActivity implements View.OnClickListener, OnNetResultListener {
    private LinearLayout mBackLay;
    private TextView mBackContentTv;
    private TextView mHeaderTitleTv;
    private RelativeLayout mHeaderRightRlay;
    private RelativeLayout mDefaultRl;
    private EditText mNameEt;
    private EditText mPhoneEt;
    private TextView mCityTv;
    private EditText mAddressEt;
    private Button mCommitBt;
    private String name;
    private String phone;
    private String address;
    private RelativeLayout mCityRl;
    private CheckBox checkBox;
    private String pName;
    private String cName;
    private String aName;
    private String iName;
    private String iPhone;
    private String iPAC;
    private String iAddress;
    private String mIid;
    private int checked;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_user_addaddress);
        ((GlobalApplication) getApplication()).getActivityManager().addActivity("MeCompileAddressActivity", this);

        initview();
        initData();
        EventBus.getDefault().register(this);

    }

    @Subscribe
    public void onEvent(AddressEvent event){
        pName = event.getpName();
        cName = event.getcName();
        aName = event.getaName();
        mCityTv.setText(pName + cName + aName);
    }


    private void initview() {
        mBackLay = (LinearLayout) findViewById(R.id.llay_header_back);
        mBackContentTv = (TextView) findViewById(R.id.tv_header_backcontent);
        mHeaderTitleTv = (TextView) findViewById(R.id.tv_header_title);
        mHeaderRightRlay = (RelativeLayout) findViewById(R.id.rlay_header_rightfunc);
        mNameEt = (EditText) findViewById(R.id.et_add_name);
        mPhoneEt = (EditText) findViewById(R.id.et_add_phone);
        mCityTv = (TextView) findViewById(R.id.et_add_city);
        mAddressEt = (EditText) findViewById(R.id.et_add_address);
        mCommitBt = (Button) findViewById(R.id.btn_commit);
        mCityRl = (RelativeLayout) findViewById(R.id.rl_city);
        checkBox = (CheckBox) findViewById(R.id.checkBox);
        mDefaultRl = (RelativeLayout) findViewById(R.id.rl_me_dafault);

        mBackContentTv.setText("返回");
        mHeaderTitleTv.setText("修改收货地址");
        mHeaderRightRlay.setVisibility(View.GONE);
        mDefaultRl.setVisibility(View.INVISIBLE);

        mBackLay.setOnClickListener(this);
        mCommitBt.setOnClickListener(this);
        mCityRl.setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void initData() {
        Intent intent = this.getIntent();
        iName = intent.getStringExtra("mName");
        iPhone = intent.getStringExtra("mPhone");
        iPAC = intent.getStringExtra("mPCA");
        iAddress = intent.getStringExtra("mAddress");
        mIid = intent.getStringExtra("mIid");

        mNameEt.setText(iName);
        mPhoneEt.setText(iPhone);
        mCityTv.setText(iPAC);
        mAddressEt.setText(iAddress);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llay_header_back: //点击返回,关闭界面
                finish();
                break;
            case R.id.rl_city: //设置您的收货地址,则添加收货地址
                Intent intent = new Intent(this, MeSelectCityActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_commit: //点击提交按钮,添加地址
                name = mNameEt.getText().toString().trim();
                phone = mPhoneEt.getText().toString().trim();
                address = mAddressEt.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    CUtils.showToast("收货人的姓名不能为空");
                    return;
                } else if (TextUtils.isEmpty(phone)) {
                    CUtils.showToast("收货人的电话不能为空");
                    return;
                } else if (mCityTv.getText().equals("请设置您的收货地址")) {
                    CUtils.showToast("收货地址不能为空");
                    return;
                } else if (TextUtils.isEmpty(address)) {
                    CUtils.showToast("详细地址不能为空");
                    return;
                }
                // 使用正则匹配手机号是否合格
                Pattern p = Pattern.compile("[1][34578]\\d{9}");
                Matcher m = p.matcher(phone);
                boolean rs = m.matches();
                if (!rs) {
                    CUtils.showToast("您输入的手机号码格式有误,请重新输入");
                    return;
                }
                if(name.equals(iName) && phone.equals(iPhone) && mCityTv.getText().equals(iPAC)
                        && address.equals(iAddress)) {
                    CUtils.showToast("请您修改您的收货信息");
                    return;
                }
                //发起网络请求,向服务器添加地址
                String url = UrlDealUtil.getRealUrlNoId(RequestCode.UPDATE_RECEIPTADDRESS_1);
                HashMap requestParams = new HashMap();
                requestParams.put("i_Id", "" + mIid);
                requestParams.put("nvc_Consignee", "" + name);
                requestParams.put("nvc_MobilePhone", "" + phone);
                requestParams.put("nvc_StreetAddress", "" + address);
                if(pName != null && cName != null && aName != null) {
                    requestParams.put("pName", "" + pName);
                    requestParams.put("cName", "" + cName);
                    requestParams.put("aName", "" + aName);
                }
                HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                        this, CommonResultBean.class, RequestCode.UPDATE_RECEIPTADDRESS_1, requestParams);
                HttpHandleUtil.executeCommonPost(this, httpRequestBean);

                break;
        }
    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        Toast.makeText(this, "修改地址成功", Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public void onError(ErrorBean errorBean) {
        Toast.makeText(this, "修改地址失败", Toast.LENGTH_SHORT).show();
    }
}
